package cn.jjxx.modules.activiti.extend;

import java.util.List;
import java.util.Map;

import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.CustomProperty;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.UserTask;
import org.activiti.editor.language.json.converter.BaseBpmnJsonConverter;
import org.activiti.editor.language.json.converter.UserTaskJsonConverter;
import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ExtUserTaskJsonConverter extends UserTaskJsonConverter{
	
	@Override
    protected void convertElementToJson(ObjectNode propertiesNode, BaseElement baseElement) {
		super.convertElementToJson(propertiesNode, baseElement);
		UserTask userTask = (UserTask) baseElement;
		CustomProperty sortNumProperty = getCustomProperty(userTask, ExtProperty.SORT_NUM);
		CustomProperty msgProperty = getCustomProperty(userTask, ExtProperty.SEND_MSG_TEMPLET);
		if(sortNumProperty!=null){
			propertiesNode.put(ExtProperty.SORT_NUM, sortNumProperty.getSimpleValue());
		}
		if(msgProperty!=null){
			propertiesNode.put(ExtProperty.SEND_MSG_TEMPLET, msgProperty.getSimpleValue());
		}
    }

	@Override
	protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode,
			Map<String, JsonNode> shapeMap) {
		FlowElement flowElement = super.convertJsonToElement(elementNode,modelNode, shapeMap);
		UserTask userTask = (UserTask) flowElement;
        //将自己的属性添加到activiti自带的自定义属性中
        CustomProperty customProperty = new CustomProperty();
        //增加排序
        customProperty.setName(ExtProperty.SORT_NUM);
        customProperty.setSimpleValue(this.getPropertyValueAsString(ExtProperty.SORT_NUM, elementNode));
        userTask.getCustomProperties().add(customProperty);
        customProperty = new CustomProperty();
        //增加短信模板
        customProperty.setName(ExtProperty.SEND_MSG_TEMPLET);
        customProperty.setSimpleValue(this.getPropertyValueAsString(ExtProperty.SEND_MSG_TEMPLET, elementNode));
        userTask.getCustomProperties().add(customProperty);
        return userTask;
	}
	
	public static void fillTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap,
            Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
    
        fillJsonTypes(convertersToBpmnMap);
        fillBpmnTypes(convertersToJsonMap);
    }
  
    public static void fillJsonTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap) {
        convertersToBpmnMap.put(STENCIL_TASK_USER, ExtUserTaskJsonConverter.class);
    }
  
    public static void fillBpmnTypes(Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
        convertersToJsonMap.put(UserTask.class, ExtUserTaskJsonConverter.class);
    }
    
    public CustomProperty getCustomProperty(UserTask userTask,String propertyName) {
		List<CustomProperty> list = userTask.getCustomProperties();
		for(CustomProperty cp:list){
			if(cp.getName().equals(propertyName)){
				return cp;
			}
		}
		return null;
	}
}
